﻿using gptDataType.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace gptDataType.Interface
{
   public abstract class PlcMemberBase : IPlcMember
    {

        //变量名称
        public string Name { get; set; }
        //变量全名
        public string FullName { get; set; }
        //变量符号
        public string Symbol { get; set; }
        //数据类型
        public DataType DataType { get; set; }
        //变量地址偏移量
        public PlcAddress Address { get; set; } = new PlcAddress(0, 0);
        //占用位数
        public int SizeInBits { get; set; }

        //上一级变量
        public IPlcMember Parent { get; set; }

        /// <summary>
        /// 根据占用位数计算占用字节数
        /// </summary>
        /// <returns></returns>
        public int SizeInBytes() => (SizeInBits + 7) / 8;

        public PlcMemberBase(string name,  DataType type,  int sizeInBits, PlcAddress address = null)
        {
            Name = name;
            DataType = type;
            SizeInBits = sizeInBits;
            if (address!= null)
            {
            Address = new PlcAddress(address.ByteOffset,address.BitOffset);
            }
        }
    }
 
}
